package demo.sort;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lingyun.wang on @date 2021/12/27
 */
public class SortSet<T extends Map<String,Object>> {
    private Set<Map<String,Object>> sortSet;
    private static List<SortBO> sortBOS;

    private static void setSortBOS(List<SortBO> sortBOS) {
        SortSet.sortBOS = sortBOS;
    }

    public SortSet(Collection<Map<String, Object>> array, List<SortBO> sortBOS) {
        this.sortSet = Sets.newHashSet(array);
        setSortBOS(sortBOS);
    }
    public List<Map<String,Object>>  sort(){
        List<Map<String,Object>> list = sortSet.stream().sorted(SortSet::compareTo).collect(Collectors.toList());
        return list;
    }

    public static int compareTo(Map<String, Object> o1,Map<String, Object> o2){
        int result = -1;
        for (SortBO sortBO : sortBOS) {

            if(o1.get(sortBO.getField()) instanceof Integer){
                result = ((Integer)o1.get(sortBO.getField())).compareTo((Integer)o2.get(sortBO.getField()));
            }else {
                result = o1.get(sortBO.getField()).toString().compareTo(o2.get(sortBO.getField()).toString());
            }
            System.out.println("compareTo"+o1.get(sortBO.getField())+","+o2.get(sortBO.getField())+","+result);
            if(result > 0){
                return result;
            }
        }
        return result;
    }

    public List<Map<String, Object>> toList() {
        return Lists.newArrayList(sortSet);
    }
}
